/*
 * @Author: Jason Liu
 * @Date: 2025-03-17 17:33:27
 * @Desc: 
 */

const { queryAllQuotaData } = require("w-vue-chongqing/src/api/metadata");

export default {
  components: {},
  props: {
    set: {
      type: Boolean,
      default: false
    },
    value: {
      type: [String, Number],
      default: undefined
    }
  },
  data() {
    return {
      loading: false,
      isView: this.set,
      dataList: [],
      menuList: [],
      pageKey: 1,
      pager: {
        pageNum: 1,
        pageSize: 1000000,
        queryParams: {
          ORDER_seqNo_asc: "0"
        }
      },
      activeId: this.value
    };
  },
  created() {
    this.init();
  },
  methods: {
    /**
     * @Author: Jason Liu
     * @description: 行数据点击事件
     */
    cellClickEvent(row) {
      if (row.modelType == "quota") {
        if (this.activeId != row.id) {
          this.activeId = row.id;
        } else {
          this.activeId = null;
        }
      }
    },
    /**
     * @Author: Jason Liu
     * @description: 获取选中行样式
     */
    cellClassName({ row, rowIndex, column, columnIndex }) {
      if (row.id == this.activeId) {
        return "active_row";
      } else {
        return "";
      }
    },
    /**
     * @Author: Jason Liu
     * @description: 获取数据内容信息
     */
    getDataList() {
      this.loading = true;
      queryAllQuotaData({ keyWords: "" })
        .then(req => {
          this.menuList = req.data.map(item => {
            return {
              ...item
            };
          });
          this.searchData();
        })
        .finally(() => {
          this.loading = false;
        });
    },
    /**
     * @Author: Jason Liu
     * @description: 查询数据信息
     */
    searchData(e) {
      let menuList = this.menuList;
      if (e) {
        const valkey = e.toLocaleLowerCase();
        menuList = menuList.filter(item => {
          return (
            item.name.toLocaleLowerCase().indexOf(valkey) > -1 ||
            item.code.toLocaleLowerCase().indexOf(valkey) > -1
          );
        });
      }
      this.dataList = menuList;
      this.pageKey++;
    },
    /**
     * @Author: Jason Liu
     * @description: 确认修改数据事件
     */
    changeData() {
      this.isView = false;
      if (this.activeId) {
        let data = this.dataList.find(item => {
          return item.id == this.activeId;
        });

        this.$emit("input", this.activeId);
        this.$emit("change", data, this.activeId);
      }
    },
    /**
     * @Author: Jason Liu
     * @description: 初始化弹窗系统
     */
    init() {
      if (this.isView) {
        if (this.pageKey == 1 || this.menuList.length == 0) {
          this.getDataList();
        }
      }
    }
  },
  watch: {
    set(val) {
      this.isView = val;
      this.init();
    },
    isView(val) {
      this.$emit("update:set", val);
    },
    value(val) {
      this.activeId = val;
    }
  }
};
